define(['config', 'text!app/module/system/materialRepertory.html', 'laypage',
        'mydialog', 'autocomplete', 'jqGrid', 'common', 'ztree', 'ztreecheck',
        'css!app/assets/lib/ztree/css/demo.css',
        'css!app/assets/lib/ztree/css/zTreeStyle/zTreeStyle.css'
    ],
    function (config, viewHTML, laypage, mydialog, autocomplete, jqGrid,
              common, ztree, ztreecheck) {
        var token = (cache.go("token") === 'undefined') ? 'none' : cache.go("token");

        var grid_url = "/api/goods";
        var model = avalon.define({
            $id: "materialRepertory",
            query: {
                name: "",
                type: ""
            },
            type: [
            {
                key: "请选择",
                val: ""
            },
            {
                key: "成品",
                val: "CP"
            }, {
                key: "原料",
                val: "YL"
            }, {
                key: "辅料",
                val: "FL"
            }
        ],
            init: function () {
                model.loadGrid();
                model.addEvent();
            },
            loadGrid: function () {

                var i = Public.setGrid();
                $("#grid").jqGrid({
                    //token验证
                    loadBeforeSend: function (jqXHR) {
                        jqXHR.setRequestHeader(
                            "X-AuthToken", token);
                    },
                    datatype: "JSON",
                    mtype: "POST",
                    url: grid_url + "/list",
                    postData: model.query.$model,
                    loadComplete: function (data) {
                    },
                    loadError: function (xhr, status, error) {
                        alert(JSON.parse(xhr.responseText).msg);
                    },
                    colModel: [
                        {
                            name: "sku",
                            label: "SKU",
                            width: '100%'
                        },
                        {
                            name: "name",
                            label: "品名",
                            width: '100%'
                        },
                        {
                            name: "model",
                            label: "规格",
                            width: '100%'
                        },
                        {
                            name: "inStockQty",
                            label: "在库数量",
                            width: '100%'
                        },
                        {
                            name: "consumption",
                            label: "用量",
                            width: '100%'
                        },
                        {
                            name: "unit",
                            label: "计量单位",
                            width: '77%',
                            formatter: function (value, options, rowData) {
                                if (value == "011") {
                                    return "件";
                                }
                            }
                        },
                        {
                            name: "color",
                            label: "颜色",
                            width: '60%'
                        }, {
                            name: "type",
                            label: "类型",
                            width: '60%',
                            formatter: function (value) {
                                switch (value) {
                                    case 'YL':
                                        return "原料";
                                        break;
                                    case 'CP':
                                        return "成品";
                                        break;
                                    case  'FL':
                                        return "辅料";
                                        break;
                                    default :
                                        return value;
                                        break;
                                }
                            }
                        }
                    ],
                    cmTemplate: {
                        sortable: false,
                        title: false
                    },
                    //分页
                    page: 1,
                    rowNum: 10,
                    rowList: [10, 20, 30],
                    height: i.h,
                    autowidth: true,
                    pager: '#page',
                    sortname: 'id',
                    viewrecords: true,
                    forceFit: false,
                    rownumbers: true,
                    multiselect: true,
                    multiboxonly: true,
                    sortorder: "desc",
                    jsonReader: {
                        root: "data.list",
                        records: "data.totalRow",
                        total: "data.totalPage",
                        repeatitems: false,
                        id: "id"
                    },
                    loadError: function () {
                        Public.tips({
                            type: 2,
                            content: "加载数据出现异常！"
                        })
                    }

                });

            },

            //提交
            reloadData: function () {

                $("#grid").jqGrid("setGridParam", {
                    url: grid_url + "/list",
                    datatype: "json",
                    mtype: 'POST',
                    postData: model.query.$model
                }).trigger("reloadGrid");
            },
            //重置
            reset: function () {
                model.query = {
                    name: "",
                    type:""
                };
                model.type =  [
                    {
                        key: "请选择",
                        val: ""
                    },
                    {
                        key: "成品",
                        val: "CP"
                    }, {
                        key: "原料",
                        val: "YL"
                    }, {
                        key: "辅料",
                        val: "FL"
                    }
                ];
                model.reloadData();
            },

            addEvent: function () {
                //提交
                $("#search").click(function (e) {
                    //先阻止浏览器默认跳转行为
                    e.preventDefault();
                    model.reloadData();
                });

                //重置
                $("#resets").click(function (e) {
                    e.preventDefault();
                    model.reset();
                    model.reloadData();
                });
            }


        });

        function errorDialog(data) {
            var mydialog = dialog({
                title: "错误",
                content: "操作失败！",
                okValue: "确定",
                ok: function () {
                }
            });
            return mydialog;
        }

        function successDialog() {
            var mydialog = dialog({
                title: "成功",
                content: "操作成功！",
                okValue: "确定",
                ok: function () {
                }
            });
            return mydialog;
        }

        // 定义视图模块
        var viewmod = {
            viewtSrc: viewHTML,
            viewInit: function () {
                model.init();
            }, // 模块view加载完成后的回调方法，在刷新（F5）时可能调用两次
            viewDestroy: function () {
            } // 切换菜单时销毁原来的view
        };
        return viewmod;


    });
	
	
	

